package com.wwdx.controller.rest;

import com.google.common.collect.Maps;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxCommentModel;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.utils.ConstantHolder;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/5/23 18:54
 */
@RestController
@RequestMapping("/product/comment")
@Api(tags = "评论接口")
public class RestCommentController extends BaseController{

    /**
     * 评论列表
     */
    @RequestMapping("/list")
    @ApiOperation(value = "评论列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "评论人",name = "openId",required = true),
            @ApiImplicitParam(value = "产品ID",name = "productId"),
            @ApiImplicitParam(value = "推荐类型：0未推荐 1已推荐",name = "recommend"),
            @ApiImplicitParam(value = "分页参数",name = "page",defaultValue = "1"),
            @ApiImplicitParam(value = "分页参数",name = "rows",defaultValue = "10"),
    })
    public ResultBean list(String openId,Long productId,Integer recommend,
                           @RequestParam(value = "page",defaultValue = "1") int page,
                           @RequestParam(value = "rows",defaultValue = "10") int rows){
        WwdxCommentModel model = WwdxCommentModel.builder()
                .rest(true).openId(openId).productId(productId).recommend(recommend).build();
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        map.put("count",wwdxCommentService.selectModelCount(model));
        map.put("list",wwdxCommentService.selectModelList(model, page, rows));
        return renderSuccess(map);
    }

    /**
     * 添加评论
     */
    @RequestMapping("/add")
    @ApiOperation(value = "评论",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "评论人",name = "openId",required = true),
            @ApiImplicitParam(value = "评论内容",name = "content",required = true),
            @ApiImplicitParam(value = "产品ID",name = "productId",required = true),
            @ApiImplicitParam(value = "产品类型",name = "type",required = true)
    })
    public ResultBean add(Long productId,Integer type,String openId,String content){
        WwdxCommentModel model = WwdxCommentModel.builder()
                .openId(openId).productId(productId).content(content).type(type)
                .status(0).pid(0L).recommend(0).gmtCreated(new Date()).build();
        wwdxCommentService.save(model);
        return renderSuccess();
    }

    /**
     * 点赞
     * @param id 评论ID
     * @param openId 点赞人
     */
    @RequestMapping("/like")
    @ApiOperation(value = "点赞",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "评论ID",name = "id"),
            @ApiImplicitParam(value = "点赞人",name = "openId")
    })
    public ResultBean like(Long id,String openId){
        String likeKey = String.format(ConstantHolder.LIKE_USER_SET, id);
        Boolean sismember = setOperations.isMember(likeKey, openId);
        if (sismember){
            setOperations.remove(likeKey,openId);
        }else {
            setOperations.add(likeKey,openId);
        }
        return renderSuccess();
    }

    /**
     * 获取点赞数
     * @param id 评论ID
     * @param openId 点赞人
     */
    @RequestMapping("/getLikeCount")
    @ApiOperation(value = "评论点赞数",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "评论ID",name = "id"),
            @ApiImplicitParam(value = "点赞人",name = "openId")
    })
    public ResultBean getLikeCount(Long id,String openId){
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        String likeKey = String.format(ConstantHolder.LIKE_USER_SET, id);
        map.put("count", setOperations.size(likeKey));
        map.put("islike", setOperations.isMember(likeKey, openId));
        return renderSuccess(map);
    }
}